<!--
 * @Description: 端口镜像
 * @Author: 0001101008/HuYuxi
 * @Date: 2021-09-28 14:38:04
 * @LastEditTime: 2021-11-05 16:18:56
 * @LastEditors: 0001101008/HuYuxi
-->
<template>
  <div class="page-content" style="padding: 0">
    <div class="table-top">
      <!-- 刷新 -->
      <el-button size="medium" plain @click="refresh">刷新</el-button>
      <!-- 新建 -->
      <el-button size="medium" type="primary" plain @click="handleCreate" v-power="2">新建</el-button>
    </div>
    <tao-table
      class="table-box"
      :data="dataList"
      :page="page"
      @changePage="changePage"
      :showPage="true"
      ref="table"
    >
      <el-table-column label="镜像会话" min-width="200" prop="session" />
      <el-table-column label="镜像端口" align="center" width="150" prop="mirrorPortOid">
        <template slot-scope="scope">
          <span v-if="scope.row.extAttrs">{{scope.row.extAttrs.mirrorPort.toUpperCase()}}</span>
        </template>
      </el-table-column>
      <el-table-column label="镜像端口类型" align="center" width="150" prop="mirrorPortType">
        <template slot-scope="scope">
          <span v-if="scope.row.mirrorPortType">{{scope.row.mirrorPortType}}</span>
        </template>
      </el-table-column>
      <el-table-column label="被镜像端口" width="130" align="center" prop="mirror[0].mirroredPortOid">
        <template slot-scope="scope">
          <span
            v-if="scope.row.extAttrs"
          >{{scope.row.extAttrs.mirroredPort[scope.row.mirror[0].mirroredPortOid].toUpperCase()}}</span>
        </template>
      </el-table-column>
      <el-table-column label="被镜像端口类型" align="center" width="150" prop="mirrorPortType">
        <template slot-scope="scope">
          <span v-if="scope.row.mirror[0]">{{scope.row.mirror[0].mirroredPortType}}</span>
        </template>
      </el-table-column>
      <el-table-column label="方向" width="130" align="center" prop="mirror[0].direction" sortable>
        <template slot-scope="scope">
          <span v-if="scope.row.mirror[0].direction === 'BOTH'">双向</span>
          <span v-if="scope.row.mirror[0].direction === 'RX'">收镜像</span>
          <span v-if="scope.row.mirror[0].direction === 'TX'">发镜像</span>
        </template>
      </el-table-column>
      <!-- 操作 -->
      <el-table-column fixed="right" align="center" label="操作" width="150px">
        <!-- 编辑  删除-->
        <template slot-scope="scope">
          <el-button
            type="text"
            icon="el-icon-edit"
            @click="handleEdit(scope.row)"
            v-power="ne.dgId === 100000 ? 0 : 2"
          >编辑</el-button>
          <el-button
            type="text"
            style="color: #FA6962"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-power="ne.dgId === 100000 ? 0 : 2"
          >删除</el-button>
        </template>
      </el-table-column>
    </tao-table>
    <forms
      :ne="ne"
      :type="type"
      :dialogStatus="dialogStatus"
      :portList="portList"
      @handleDialog="handleDialog"
      v-if="dialogStatus"
    ></forms>
    <update
      :ne="ne"
      :type="type"
      :dialogStatus="dialogUpdateStatus"
      :portInfo="portInfo"
      :portList="portList"
      @handleUpdateDialog="handleUpdateDialog"
      v-if="dialogUpdateStatus"
    ></update>
  </div>
</template>
<script>
import forms from './forms';
import update from './update';
export default {
  props: ['ne'],
  components: {
    forms,
    update
  },
  data () {
    return {
      dataList: [],
      page: {
        page: 1,
        pageSize: 15,
        total: 0
      },
      type: 'create',
      dialogStatus: false,
      dialogUpdateStatus: false,
      portInfo: {},
      portList: []
    };
  },
  mounted () {
    let _this = this;
    _this.$nextTick(() => {
      _this.getProtList();
      _this.refresh();
    });
  },
  methods: {
    // 切换页码 & 显示条数
    changePage (page) {
      if (page.page) {
        this.page.page = page.page;
      } else {
        this.page.page = 1;
        this.page.pageSize = page.limit;
      }
      this.refresh();
    },
    // 新建
    handleCreate () {
      let _this = this;
      _this.type = 'create';
      _this.portInfo = {};
      _this.dialogStatus = true;
    },
    // 编辑
    handleEdit (row) {
      let _this = this;
      _this.type = 'edit';
      _this.portInfo = row;
      _this.dialogUpdateStatus = true;
    },
    // 新建的回调
    handleDialog (data) {
      let _this = this;
      _this.type = 'create';
      _this.dialogStatus = data;
    },
    // 编辑的回调
    handleUpdateDialog (data) {
      let _this = this;
      _this.type = 'create';
      _this.dialogUpdateStatus = data;
    },
    // 刷新
    refresh () {
      let _this = this;
      let params = {
        commandCode: 'maintenance.portflowmirror.get',
        paras: {}
      };
      _this.$http.post({
        url: '/api/resource/nemng/command/' + _this.ne.id,
        data: params
      }).then(res => {
        _this.dataList = res;
      });
    },
    // 获取接口
    getProtList () {
      let _this = this;
      let params = {
        commandCode: 'intfselector.get',
        paras: {
          limit: 0,
          offset: 0,
          use: 'RouterPortMirror'
        }
      };
      _this.$http.post({
        url: '/api/resource/nemng/command/' + _this.ne.id,
        data: params
      }).then(res => {
        _this.portList = res.objects;
      });
    },
    // 删除
    handleDelete (row) {
      let _this = this;
      this.$confirm('您确定要删除当前数据吗？', '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'error'
      }).then(() => {
        _this.$http.post({
          url: '/api/resource/nemng/command/' + _this.ne.id,
          data: {
            commandCode: 'maintenance.portflowmirror.set',
            paras: {
              removes: [row.session]
            }
          }
        }).then(res => {
          let { removes } = res;
          if (removes) {
            _this.$message.success('删除成功！');
            _this.refresh();
            _this.getProtList();
          }
        });
      }).catch(() => {
        _this.$message.info('取消删除');
      });
    }
  }
};
</script>
<style lang='scss' scoped>
.page-content {
  width: 100%;
  height: 100%;
  .table-top {
    margin-bottom: 15px;
  }
  .table-box {
    ::v-deep .el-button {
      padding: 0;
    }
    ::v-deep .el-dropdown-link {
      cursor: pointer;
      color: #409eff;
    }
  }
}
</style>